import { defineStore } from 'pinia'

// 用户信息Store
export const useUserStore = defineStore('user', {
  state: () => ({
    userInfo: null,
    token: null,
    isLoggedIn: false
  }),
  
  getters: {
    // 获取用户信息
    getUserInfo: (state) => state.userInfo,
    
    // 检查是否登录
    getIsLoggedIn: (state) => state.isLoggedIn,
    
    // 获取用户角色
    getUserRole: (state) => state.userInfo?.role || 'guest'
  },
  
  actions: {
    // 设置用户信息
    setUserInfo(userInfo) {
      this.userInfo = userInfo
      this.isLoggedIn = true
      
      // 保存到本地存储
      localStorage.setItem('userInfo', JSON.stringify(userInfo))
    },
    
    // 设置token
    setToken(token) {
      this.token = token
      this.isLoggedIn = true
      
      // 保存到本地存储
      localStorage.setItem('token', token)
    },
    
    // 登录
    login(userInfo, token) {
      this.setUserInfo(userInfo)
      this.setToken(token)
    },
    
    // 注销
    logout() {
      this.userInfo = null
      this.token = null
      this.isLoggedIn = false
      
      // 清除本地存储
      localStorage.removeItem('userInfo')
      localStorage.removeItem('token')
    },
    
    // 从本地存储加载用户信息
    loadUserFromStorage() {
      const token = localStorage.getItem('token')
      const userInfoStr = localStorage.getItem('userInfo')
      
      if (token && userInfoStr) {
        try {
          const userInfo = JSON.parse(userInfoStr)
          this.setUserInfo(userInfo)
          this.setToken(token)
        } catch (error) {
          console.error('解析用户信息失败:', error)
          this.logout()
        }
      }
    }
  }
})

// 学生信息Store
export const useStudentStore = defineStore('student', {
  state: () => ({
    students: [],
    total: 0,
    loading: false,
    currentPage: 1,
    pageSize: 10,
    searchQuery: '',
    classFilter: ''
  }),
  
  getters: {
    // 获取学生列表
    getStudents: (state) => state.students,
    
    // 获取总条数
    getTotal: (state) => state.total,
    
    // 获取加载状态
    getLoading: (state) => state.loading,
    
    // 获取分页信息
    getPagination: (state) => ({
      currentPage: state.currentPage,
      pageSize: state.pageSize,
      total: state.total
    })
  },
  
  actions: {
    // 设置学生列表
    setStudents(students) {
      this.students = students
    },
    
    // 设置总条数
    setTotal(total) {
      this.total = total
    },
    
    // 设置加载状态
    setLoading(loading) {
      this.loading = loading
    },
    
    // 设置当前页码
    setCurrentPage(page) {
      this.currentPage = page
    },
    
    // 设置每页条数
    setPageSize(size) {
      this.pageSize = size
    },
    
    // 设置搜索条件
    setSearchQuery(query) {
      this.searchQuery = query
    },
    
    // 设置班级筛选
    setClassFilter(classId) {
      this.classFilter = classId
    },
    
    // 重置筛选条件
    resetFilters() {
      this.searchQuery = ''
      this.classFilter = ''
      this.currentPage = 1
    },
    
    // 添加学生
    addStudent(student) {
      this.students.unshift(student)
      this.total++
    },
    
    // 更新学生
    updateStudent(id, updatedStudent) {
      const index = this.students.findIndex(student => student.id === id)
      if (index !== -1) {
        this.students[index] = { ...this.students[index], ...updatedStudent }
      }
    },
    
    // 删除学生
    deleteStudent(id) {
      this.students = this.students.filter(student => student.id !== id)
      this.total--
    },
    
    // 批量删除学生
    batchDeleteStudents(ids) {
      this.students = this.students.filter(student => !ids.includes(student.id))
      this.total -= ids.length
    }
  }
})

// 班级信息Store
export const useClassStore = defineStore('class', {
  state: () => ({
    classes: [],
    total: 0,
    loading: false,
    currentPage: 1,
    pageSize: 10,
    searchQuery: ''
  }),
  
  getters: {
    // 获取班级列表
    getClasses: (state) => state.classes,
    
    // 获取总条数
    getTotal: (state) => state.total,
    
    // 获取加载状态
    getLoading: (state) => state.loading
  },
  
  actions: {
    // 设置班级列表
    setClasses(classes) {
      this.classes = classes
    },
    
    // 设置总条数
    setTotal(total) {
      this.total = total
    },
    
    // 设置加载状态
    setLoading(loading) {
      this.loading = loading
    },
    
    // 设置当前页码
    setCurrentPage(page) {
      this.currentPage = page
    },
    
    // 设置每页条数
    setPageSize(size) {
      this.pageSize = size
    },
    
    // 设置搜索条件
    setSearchQuery(query) {
      this.searchQuery = query
    },
    
    // 重置筛选条件
    resetFilters() {
      this.searchQuery = ''
      this.currentPage = 1
    },
    
    // 添加班级
    addClass(classData) {
      this.classes.unshift(classData)
      this.total++
    },
    
    // 更新班级
    updateClass(id, updatedClass) {
      const index = this.classes.findIndex(classItem => classItem.id === id)
      if (index !== -1) {
        this.classes[index] = { ...this.classes[index], ...updatedClass }
      }
    },
    
    // 删除班级
    deleteClass(id) {
      this.classes = this.classes.filter(classItem => classItem.id !== id)
      this.total--
    }
  }
})

// 课程信息Store
export const useCourseStore = defineStore('course', {
  state: () => ({
    courses: [],
    total: 0,
    loading: false,
    currentPage: 1,
    pageSize: 10,
    searchQuery: '',
    categoryFilter: ''
  }),
  
  getters: {
    // 获取课程列表
    getCourses: (state) => state.courses,
    
    // 获取总条数
    getTotal: (state) => state.total,
    
    // 获取加载状态
    getLoading: (state) => state.loading
  },
  
  actions: {
    // 设置课程列表
    setCourses(courses) {
      this.courses = courses
    },
    
    // 设置总条数
    setTotal(total) {
      this.total = total
    },
    
    // 设置加载状态
    setLoading(loading) {
      this.loading = loading
    },
    
    // 设置当前页码
    setCurrentPage(page) {
      this.currentPage = page
    },
    
    // 设置每页条数
    setPageSize(size) {
      this.pageSize = size
    },
    
    // 设置搜索条件
    setSearchQuery(query) {
      this.searchQuery = query
    },
    
    // 设置类别筛选
    setCategoryFilter(category) {
      this.categoryFilter = category
    },
    
    // 重置筛选条件
    resetFilters() {
      this.searchQuery = ''
      this.categoryFilter = ''
      this.currentPage = 1
    },
    
    // 添加课程
    addCourse(course) {
      this.courses.unshift(course)
      this.total++
    },
    
    // 更新课程
    updateCourse(id, updatedCourse) {
      const index = this.courses.findIndex(course => course.id === id)
      if (index !== -1) {
        this.courses[index] = { ...this.courses[index], ...updatedCourse }
      }
    },
    
    // 删除课程
    deleteCourse(id) {
      this.courses = this.courses.filter(course => course.id !== id)
      this.total--
    }
  }
})

// 系统设置Store
export const useSystemStore = defineStore('system', {
  state: () => ({
    sidebarCollapsed: false,
    loading: false,
    notification: null,
    theme: 'default',
    layout: 'side'
  }),
  
  getters: {
    // 获取侧边栏状态
    getSidebarCollapsed: (state) => state.sidebarCollapsed,
    
    // 获取加载状态
    getLoading: (state) => state.loading,
    
    // 获取通知
    getNotification: (state) => state.notification,
    
    // 获取主题
    getTheme: (state) => state.theme,
    
    // 获取布局
    getLayout: (state) => state.layout
  },
  
  actions: {
    // 切换侧边栏
    toggleSidebar() {
      this.sidebarCollapsed = !this.sidebarCollapsed
    },
    
    // 设置侧边栏状态
    setSidebarCollapsed(collapsed) {
      this.sidebarCollapsed = collapsed
    },
    
    // 设置加载状态
    setLoading(loading) {
      this.loading = loading
    },
    
    // 设置通知
    setNotification(notification) {
      this.notification = notification
      
      // 3秒后自动清除通知
      if (notification) {
        setTimeout(() => {
          this.notification = null
        }, 3000)
      }
    },
    
    // 清除通知
    clearNotification() {
      this.notification = null
    },
    
    // 设置主题
    setTheme(theme) {
      this.theme = theme
      localStorage.setItem('theme', theme)
    },
    
    // 设置布局
    setLayout(layout) {
      this.layout = layout
      localStorage.setItem('layout', layout)
    },
    
    // 从本地存储加载设置
    loadSettingsFromStorage() {
      const theme = localStorage.getItem('theme')
      const layout = localStorage.getItem('layout')
      
      if (theme) {
        this.setTheme(theme)
      }
      
      if (layout) {
        this.setLayout(layout)
      }
    }
  }
})